################################################################################################
# This script takes the raw QV/SV data from the splitting script and provides a breakdown of 
# the voting results. In the case of QV it uses the voting weights given to subjects. 
# Otherwise the votes count as a single vote (SV). 
# 
# Exports two summary dataframes for QV and SV. 
# 
# Author: Luis S.
# Last Modified: 7.23.16
################################################################################################

library(dplyr)

# setwd("C:/Users/Luis/Dropbox/Research/Casella/Storable Votes (MTurk) - California Experiment/California Experiment Results/")

####################################
#### SV Data Processing & Count ####
####################################

SV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_SV_Stage_2_clean.csv", header = T, stringsAsFactors = F)

# Determine if bonus vote was in favor or against
SV_data <- mutate(SV_data, SV1_VotePosition = NA, SV2_VotePosition = NA)

for(row in 1:nrow(SV_data))
{
  # Mutate dataframe with bonus vote position
  if(SV_data$SV1_Vote[row] == "BilingualEduc") {SV_data$SV1_VotePosition[row] <- SV_data$Ref1_BilingualEduc[row]}
  if(SV_data$SV1_Vote[row] == "TeacherTenure") {SV_data$SV1_VotePosition[row] <- SV_data$Ref2_TeacherTenure[row]}
  if(SV_data$SV1_Vote[row] == "PublicBonds") {SV_data$SV1_VotePosition[row] <- SV_data$Ref3_PublicBonds[row]}
  if(SV_data$SV1_Vote[row] == "Immigration") {SV_data$SV1_VotePosition[row] <- SV_data$Ref4_Immigration[row]}
  
  if(SV_data$SV2_Vote[row] == "BilingualEduc") {SV_data$SV2_VotePosition[row] <- SV_data$Ref1_BilingualEduc[row]}
  if(SV_data$SV2_Vote[row] == "TeacherTenure") {SV_data$SV2_VotePosition[row] <- SV_data$Ref2_TeacherTenure[row]}
  if(SV_data$SV2_Vote[row] == "PublicBonds") {SV_data$SV2_VotePosition[row] <- SV_data$Ref3_PublicBonds[row]}
  if(SV_data$SV2_Vote[row] == "Immigration") {SV_data$SV2_VotePosition[row] <- SV_data$Ref4_Immigration[row]}
}


# Determine count w/o bonus votes
tempRef1 <- group_by(SV_data, Ref1_BilingualEduc) %>% summarise(BilingualEduc = n())
tempRef2 <- group_by(SV_data, Ref2_TeacherTenure) %>% summarise(TeacherTenure = n())
tempRef3 <- group_by(SV_data, Ref3_PublicBonds) %>% summarise(PublicBonds = n())
tempRef4 <- group_by(SV_data, Ref4_Immigration) %>% summarise(Immigration = n())

SV_summary <- data.frame(Position = c("Abstain", "Against", "InFavor"))
SV_summary$BilingualEduc_NoBV <- tempRef1$BilingualEduc
SV_summary$TeacherTenure_NoBV <- tempRef2$TeacherTenure
SV_summary$PublicBonds_NoBV <- tempRef3$PublicBonds
SV_summary$Immigration_NoBV <- tempRef4$Immigration

# Determine count w/ SV1 & SV2 bonus votes
tempBV1 <- group_by(SV_data, SV1_Vote, SV1_VotePosition) %>% summarise(BV1 = n())

tempAbs <- filter(tempBV1, SV1_Vote == "BilingualEduc", SV1_VotePosition == "Abstain")$BV1
tempAgainst <- filter(tempBV1, SV1_Vote == "BilingualEduc", SV1_VotePosition == "Against")$BV1
tempInFav <- filter(tempBV1, SV1_Vote == "BilingualEduc", SV1_VotePosition == "InFavor")$BV1
SV_summary$BilingualEduc_onlyBV1[1] <- ifelse(length(tempAbs) > 0, tempAbs, 0)
SV_summary$BilingualEduc_onlyBV1[2] <- ifelse(length(tempAgainst) > 0, tempAgainst, 0)
SV_summary$BilingualEduc_onlyBV1[3] <- ifelse(length(tempInFav) > 0, tempInFav, 0)

tempAbs <- filter(tempBV1, SV1_Vote == "TeacherTenure", SV1_VotePosition == "Abstain")$BV1
tempAgainst <- filter(tempBV1, SV1_Vote == "TeacherTenure", SV1_VotePosition == "Against")$BV1
tempInFav <- filter(tempBV1, SV1_Vote == "TeacherTenure", SV1_VotePosition == "InFavor")$BV1
SV_summary$TeacherTenure_onlyBV1[1] <- ifelse(length(tempAbs) > 0, tempAbs, 0)
SV_summary$TeacherTenure_onlyBV1[2] <- ifelse(length(tempAgainst) > 0, tempAgainst, 0)
SV_summary$TeacherTenure_onlyBV1[3] <- ifelse(length(tempInFav) > 0, tempInFav, 0)

tempAbs <- filter(tempBV1, SV1_Vote == "PublicBonds", SV1_VotePosition == "Abstain")$BV1
tempAgainst <- filter(tempBV1, SV1_Vote == "PublicBonds", SV1_VotePosition == "Against")$BV1
tempInFav <- filter(tempBV1, SV1_Vote == "PublicBonds", SV1_VotePosition == "InFavor")$BV1
SV_summary$PublicBonds_onlyBV1[1] <- ifelse(length(tempAbs) > 0, tempAbs, 0)
SV_summary$PublicBonds_onlyBV1[2] <- ifelse(length(tempAgainst) > 0, tempAgainst, 0)
SV_summary$PublicBonds_onlyBV1[3] <- ifelse(length(tempInFav) > 0, tempInFav, 0)

tempAbs <- filter(tempBV1, SV1_Vote == "Immigration", SV1_VotePosition == "Abstain")$BV1
tempAgainst <- filter(tempBV1, SV1_Vote == "Immigration", SV1_VotePosition == "Against")$BV1
tempInFav <- filter(tempBV1, SV1_Vote == "Immigration", SV1_VotePosition == "InFavor")$BV1
SV_summary$Immigration_onlyBV1[1] <- ifelse(length(tempAbs) > 0, tempAbs, 0)
SV_summary$Immigration_onlyBV1[2] <- ifelse(length(tempAgainst) > 0, tempAgainst, 0)
SV_summary$Immigration_onlyBV1[3] <- ifelse(length(tempInFav) > 0, tempInFav, 0)




tempBV2 <- group_by(SV_data, SV2_Vote, SV2_VotePosition) %>% summarise(BV2 = n())

tempAbs <- filter(tempBV2, SV2_Vote == "BilingualEduc", SV2_VotePosition == "Abstain")$BV2
tempAgainst <- filter(tempBV2, SV2_Vote == "BilingualEduc", SV2_VotePosition == "Against")$BV2
tempInFav <- filter(tempBV2, SV2_Vote == "BilingualEduc", SV2_VotePosition == "InFavor")$BV2
SV_summary$BilingualEduc_onlyBV2[1] <- ifelse(length(tempAbs) > 0, tempAbs, 0)
SV_summary$BilingualEduc_onlyBV2[2] <- ifelse(length(tempAgainst) > 0, tempAgainst, 0)
SV_summary$BilingualEduc_onlyBV2[3] <- ifelse(length(tempInFav) > 0, tempInFav, 0)

tempAbs <- filter(tempBV2, SV2_Vote == "TeacherTenure", SV2_VotePosition == "Abstain")$BV2
tempAgainst <- filter(tempBV2, SV2_Vote == "TeacherTenure", SV2_VotePosition == "Against")$BV2
tempInFav <- filter(tempBV2, SV2_Vote == "TeacherTenure", SV2_VotePosition == "InFavor")$BV2
SV_summary$TeacherTenure_onlyBV2[1] <- ifelse(length(tempAbs) > 0, tempAbs, 0)
SV_summary$TeacherTenure_onlyBV2[2] <- ifelse(length(tempAgainst) > 0, tempAgainst, 0)
SV_summary$TeacherTenure_onlyBV2[3] <- ifelse(length(tempInFav) > 0, tempInFav, 0)

tempAbs <- filter(tempBV2, SV2_Vote == "PublicBonds", SV2_VotePosition == "Abstain")$BV2
tempAgainst <- filter(tempBV2, SV2_Vote == "PublicBonds", SV2_VotePosition == "Against")$BV2
tempInFav <- filter(tempBV2, SV2_Vote == "PublicBonds", SV2_VotePosition == "InFavor")$BV2
SV_summary$PublicBonds_onlyBV2[1] <- ifelse(length(tempAbs) > 0, tempAbs, 0)
SV_summary$PublicBonds_onlyBV2[2] <- ifelse(length(tempAgainst) > 0, tempAgainst, 0)
SV_summary$PublicBonds_onlyBV2[3] <- ifelse(length(tempInFav) > 0, tempInFav, 0)

tempAbs <- filter(tempBV2, SV2_Vote == "Immigration", SV2_VotePosition == "Abstain")$BV2
tempAgainst <- filter(tempBV2, SV2_Vote == "Immigration", SV2_VotePosition == "Against")$BV2
tempInFav <- filter(tempBV2, SV2_Vote == "Immigration", SV2_VotePosition == "InFavor")$BV2
SV_summary$Immigration_onlyBV2[1] <- ifelse(length(tempAbs) > 0, tempAbs, 0)
SV_summary$Immigration_onlyBV2[2] <- ifelse(length(tempAgainst) > 0, tempAgainst, 0)
SV_summary$Immigration_onlyBV2[3] <- ifelse(length(tempInFav) > 0, tempInFav, 0)




SV_summary <- SV_summary %>%
  mutate(BilingualEduc_withBV1 = BilingualEduc_NoBV + BilingualEduc_onlyBV1,
         BilingualEduc_withBV2 = BilingualEduc_NoBV + BilingualEduc_onlyBV2,
         TeacherTenure_withBV1 = TeacherTenure_NoBV + TeacherTenure_onlyBV1,
         TeacherTenure_withBV2 = TeacherTenure_NoBV + TeacherTenure_onlyBV2,
         PublicBonds_withBV1 = PublicBonds_NoBV + PublicBonds_onlyBV1,
         PublicBonds_withBV2 = PublicBonds_NoBV + PublicBonds_onlyBV2,
         Immigration_withBV1 = Immigration_NoBV + Immigration_onlyBV1,
         Immigration_withBV2 = Immigration_NoBV + Immigration_onlyBV2) %>%
  select(-matches("only"))

# Determine winner of each referenda
SV_summary <- as.data.frame(t(SV_summary))
SV_summary <- SV_summary[-1,]

write.csv(SV_summary, "California Stage 2 - SV & QV Data/Summaries & Tests/SV_VoteSummary.csv")
SV_summary <- read.csv("California Stage 2 - SV & QV Data/Summaries & Tests/SV_VoteSummary.csv", header = T, stringsAsFactors = F)

names(SV_summary) <- c("Referenda", "Abstain", "Against", "InFavor")
SV_summary <- mutate(SV_summary, WinningSide = ifelse(Against > InFavor, "Against", "InFavor")) %>%
  arrange(Referenda)
write.csv(SV_summary, "California Stage 2 - SV & QV Data/Summaries & Tests/SV_VoteSummary.csv", row.names = F)


####################################
#### QV Data Processing & Count ####
####################################

QV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_QV_Stage_2_clean.csv", header = T, stringsAsFactors = F)

# Replaces votes with actual weights 
QV_data[is.na(QV_data)] <- 0

for(row in 1:nrow(QV_data))
{
  # QV1
  if(QV_data$QV1_Class[row] == "(1) Red") {voteWeight <- 2}
  else if(QV_data$QV1_Class[row] == "(2) Yellow") {voteWeight <- 1.5} 
  else if(QV_data$QV1_Class[row] == "(3) Green") {voteWeight <- 1.2} 
  else {voteWeight <- 1} 
  
  QV_data$QV1_Vote_BilingualEduc[row] <- QV_data$QV1_Vote_BilingualEduc[row] * voteWeight
  QV_data$QV1_Vote_TeacherTenure[row] <- QV_data$QV1_Vote_TeacherTenure[row] * voteWeight
  QV_data$QV1_Vote_PublicBonds[row] <- QV_data$QV1_Vote_PublicBonds[row] * voteWeight
  QV_data$QV1_Vote_Immigration[row] <- QV_data$QV1_Vote_Immigration[row] * voteWeight
  
  
  # QV2
  if(QV_data$QV2_Class[row] == "(1) Red") {voteWeight <- 2}
  else if(QV_data$QV2_Class[row] == "(2) Yellow") {voteWeight <- 1.5} 
  else if(QV_data$QV2_Class[row] == "(3) Green") {voteWeight <- 1.2} 
  else {voteWeight <- 1} 
  
  QV_data$QV2_Vote_BilingualEduc[row] <- QV_data$QV2_Vote_BilingualEduc[row] * voteWeight
  QV_data$QV2_Vote_TeacherTenure[row] <- QV_data$QV2_Vote_TeacherTenure[row] * voteWeight
  QV_data$QV2_Vote_PublicBonds[row] <- QV_data$QV2_Vote_PublicBonds[row] * voteWeight
  QV_data$QV2_Vote_Immigration[row] <- QV_data$QV2_Vote_Immigration[row] * voteWeight
}


# Determine count w/o QV
tempRef1 <- group_by(QV_data, Ref1_BilingualEduc) %>% summarise(BilingualEduc = n())
tempRef2 <- group_by(QV_data, Ref2_TeacherTenure) %>% summarise(TeacherTenure = n())
tempRef3 <- group_by(QV_data, Ref3_PublicBonds) %>% summarise(PublicBonds = n())
tempRef4 <- group_by(QV_data, Ref4_Immigration) %>% summarise(Immigration = n())

tempQV_noQV <- bind_cols(tempRef1,tempRef2,tempRef3,tempRef4) %>% select(-matches("Ref"))

# Determine Weighted vote count & add vote side w/o QV  

tempRef1 <- group_by(QV_data, Ref1_BilingualEduc) %>% 
  summarise(BilingualEduc_QV1 = sum(QV1_Vote_BilingualEduc), BilingualEduc_QV2 = sum(QV2_Vote_BilingualEduc))

tempRef2 <- group_by(QV_data, Ref2_TeacherTenure) %>% 
  summarise(TeacherTenure_QV1 = sum(QV1_Vote_TeacherTenure), TeacherTenure_QV2 = sum(QV2_Vote_TeacherTenure))

tempRef3 <- group_by(QV_data, Ref3_PublicBonds) %>% 
  summarise(PublicBonds_QV1 = sum(QV1_Vote_PublicBonds), PublicBonds_QV2 = sum(QV2_Vote_PublicBonds))

tempRef4 <- group_by(QV_data, Ref4_Immigration) %>% 
  summarise(Immigration_QV1 = sum(QV1_Vote_Immigration), Immigration_QV2 = sum(QV2_Vote_Immigration))

QV_summary <- bind_cols(tempQV_noQV,tempRef1,tempRef2,tempRef3,tempRef4) %>% select(-matches("Ref"))
QV_summary <- as.data.frame(t(QV_summary))

write.csv(QV_summary, "California Stage 2 - SV & QV Data/Summaries & Tests/QV_VoteSummary.csv")
QV_summary <- read.csv("California Stage 2 - SV & QV Data/Summaries & Tests/QV_VoteSummary.csv", header = T, stringsAsFactors = F)
names(QV_summary) <- c("Referenda", "Abstain", "Against", "InFavor")

# Determine the winning side
QV_summary <- mutate(QV_summary, WinningSide = ifelse(Against > InFavor, "Against", "InFavor")) %>%
  arrange(Referenda)

write.csv(QV_summary, "California Stage 2 - SV & QV Data/Summaries & Tests/QV_VoteSummary.csv", row.names = F)

